<?php 
$lang="VN";
$company="elitech";
//$username="abc";
//$status=1;
//$lang=$_REQUEST['lang'];
//$company=$_REQUEST['company'];
//$username=$_REQUEST['username'];
//$status=$_REQUEST['status'];

/*$lang=$_REQUEST['lang'];
$company=$_REQUEST['company'];
$username=$_REQUEST['username'];
if(isset($_REQUEST['status']))
	$status=$_REQUEST['status'];
else
	$status=1;

if($status==0){
	$l_thoigiangiaodich=$l_thoigiangiaodich0;
}
if($status==1){
	$l_thoigiangiaodich=$l_thoigiangiaodich1;
}*/


$status=1;


$connection = mysql_connect("1.234.53.52", "cam", "5678" ) ;
//$connection = mysql_connect("localhost", "root", "root" ) ;
mysql_select_db("elihan", $connection);
mysql_query("set names 'utf8'");
$selectdatabase = mysql_query("SELECT am.DatabaseName,am.Logo from ac_eli_manager am where am.KeyName='$company' and am.`Status` = 2");
$result = mysql_fetch_assoc($selectdatabase);
$db= $result['DatabaseName'];
//echo $db;
mysql_select_db($db, $connection);


if (empty($lang) || $lang == "EN") {
	$value = "value/value_en.php";
} else if($lang == "VN") {
	$value = "value/value_vn.php";
}else {
	$value = "value/value_kr.php";
}
include($value);


$p=mysql_query("SELECT tp.IDTest,tp.IDProject,tp.IDEmployee,tp.Title,tp.TextTest,tp.IDTestTemp,tp.Time,p.IDView,p.NameProject,e.FullName FROM pr_testproject tp,pr_project p,employee e
WHERE tp.IDProject=p.IDProject AND tp.IDEmployee=e.IDEmployee AND tp.IDTest=(SELECT IDTestProject FROM pr_comment c WHERE c.Sendmail=0 LIMIT 0,1);");


if(mysql_num_rows($p)>0){
	$r=mysql_fetch_array($p);
	$str="";
	$q1=mysql_query("SELECT c.*,e.FullName FROM pr_comment c,employee e WHERE c.IDTestProject=(SELECT IDTestProject FROM pr_comment c WHERE c.Sendmail=0 LIMIT 0,1) AND c.IDEmployee=e.IDEmployee;");
	$count=mysql_num_rows($q1);
	while ($r1=mysql_fetch_array($q1)){
		$str.='
			<tr style="background: #90BEE8;" height="35px">
				<td style="text-align: left;font-size: 14px;padding-left: 50px;">      -  '.substr($r1["Comment"],0,-14).'</td>
				<td style="text-align:center;font-size: 14px;">'.$r1["FullName"].'</td>
				<td style="text-align:center;font-size: 14px;">'.$r1["DateComment"].'</td>
			</tr>';
		mysql_query("UPDATE `pr_comment` SET `Sendmail`='1' WHERE (`IDComment`='".$r1['IDComment']."')");
		$username=$r1["FullName"];
		$IDEmployee=$r1["IDEmployee"];
	}
	$q=mysql_query("SELECT l.id_employee,e.Email,e.FullName FROM list_sendmailproject l,employee e WHERE l.id_main_employee='".$IDEmployee."' AND l.id_project='".$r["IDProject"]."' AND l.id_employee=e.IDEmployee");
	// file 03-gmail.php
			
			//NhÃƒÂºng thÃ†Â° viÃ¡Â»â€¡n phpmailer
			require_once('class.phpmailer.php');
			
			//KhÃ¡Â»Å¸i tÃ¡ÂºÂ¡o Ã„â€˜Ã¡Â»â€˜i tÃ†Â°Ã¡Â»Â£ng
			$mail = new PHPMailer();
			
			$mail->IsSMTP(); // GÃ¡Â»ï¿½i Ã„â€˜Ã¡ÂºÂ¿n class xÃ¡Â»Â­ lÃƒÂ½ SMTP
			//$mail->Host       = "mail.zend.vn"; // tÃƒÂªn SMTP server
			$mail->SMTPDebug  = 2;                    // enables SMTP debug information (for testing)
			                                           // 1 = errors and messages
			                                           // 2 = messages only
			$mail->SMTPAuth   = true;                  // SÃ¡Â»Â­ dÃ¡Â»Â¥ng Ã„â€˜Ã„Æ’ng nhÃ¡ÂºÂ­p vÃƒÂ o account
			$mail->SMTPSecure = "ssl";
			$mail->Host       = "smtp.gmail.com";     // ThiÃ¡ÂºÂ¿t lÃ¡ÂºÂ­p thÃƒÂ´ng tin cÃ¡Â»Â§a SMPT
			$mail->Port       = 465;                     // ThiÃ¡ÂºÂ¿t lÃ¡ÂºÂ­p cÃ¡Â»â€¢ng gÃ¡Â»Â­i email cÃ¡Â»Â§a mÃƒÂ¡y
			$mail->Username   = "elisoftmailserver@gmail.com"; // SMTP account username
			$mail->Password   = "1234567892727";         // SMTP account password
			
			//Thiet lap thong tin nguoi gui va email nguoi gui
			//$yesterday=date("Y-m-d", time() - 86400);
			$mail->SetFrom('elisoftmailserver@gmail.com',$company.' - Project : Comment');
			
			//ThiÃ¡ÂºÂ¿t lÃ¡ÂºÂ­p thÃƒÂ´ng tin ngÃ†Â°Ã¡Â»ï¿½i nhÃ¡ÂºÂ­n
	
			while ($row1=mysql_fetch_array($q)){
				$mail->AddAddress($row1['Email'], $row1['FullName']);
			}
			//$mail->AddAddress("orchipro@gmail.com", "orchipro");
			$mail->AddAddress("kisadlu@gmail.com", "meokisa");
			$mail->AddAddress("klkim169@gmail.com", "klkim");
			
			$mail->Subject    =  $company.' - Project '.$r["IDView"].'-'.$r["IDTestTemp"].' : Comment';
			
			$mail->CharSet = "utf-8";
			$mail->IsHTML(true);
			
			function download_remote_file_with_curl($file_url, $save_to)
			{
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_POST, 0); 
				curl_setopt($ch,CURLOPT_URL,$file_url); 
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
				$file_content = curl_exec($ch);
				curl_close($ch);
		 
				$downloaded_file = fopen($save_to, 'w');
				fwrite($downloaded_file, $file_content);
				fclose($downloaded_file);
		 
			}
			$jpg=array();
			
			$mang=explode('src="',$r["TextTest"]);
			for($i=1;$i<count($mang);$i++){
				$mang1=explode('"',$mang[$i]);
				$jpg[]=$mang1[0];	
			}
			
			for($i=0;$i<count($jpg);$i++){
				download_remote_file_with_curl($jpg[$i], "$i.jpg");
				$mail->AddEmbeddedImage("$i.jpg", "my-attach$i", "$i.jpg");
				$r["TextTest"]=str_replace($jpg[$i], "cid:my-attach$i", $r["TextTest"]);
				//unlink("$i.jpg");
			}
			
			$body='';
			$body.='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
					<html xmlns="http://www.w3.org/1999/xhtml">
					<head>
					<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
					<title>Untitled Document</title>
					</head>
					
					<body>
					';
			$body.='Project : '.$r['NameProject'];
			
			$body.='<br>
					<table class="ftable" width="1050px" cellspacing="1" cellpadding="0" border="0">
					<tbody>
					 <tr height="35px">
					 	<th style="min-width: 80px;background: #4f81bd;height: 30px;color: white;font-size: 14px;font-weight: bold;">Project ID</th>
					 	<th style="min-width: 80px;background: #4f81bd;height: 30px;color: white;font-size: 14px;font-weight: bold;">Project Code</th>
		                <th style="min-width: 400px;background: #4f81bd;height: 30px;color: white;font-size: 14px;font-weight: bold;">Title</th>
		                <th style="min-width: 130px;background: #4f81bd;height: 30px;color: white;font-size: 14px;font-weight: bold;">Creater</th>
		                <th style="min-width: 140px;background: #4f81bd;height: 30px;color: white;font-size: 14px;font-weight: bold;">Datetime</th>
		            </tr>
		            <tr style="background: #90BEE8;" height="35px">
		            	<td rowspan="'.($count+2).'" style="text-align:center;font-size: 14px;">'.$r["IDProject"].'</td>
		            	<td rowspan="'.($count+2).'" style="font-size: 14px;text-align: center;">'.$r["IDView"].'-'.$r["IDTestTemp"].'</td>
						<td style="text-align: left;font-size: 14px;padding-left: 10px;">'.substr($r["Title"],0,-14).'</td>
						<td rowspan="2" style="text-align:center;font-size: 14px;">'.$r["FullName"].'</td>
						<td rowspan="2" style="text-align:center;font-size: 14px;">'.$r["Time"].'</td>
					</tr>
					<tr style="background: #90BEE8;" height="35px">
		            	
						<td style="text-align: left;font-size: 14px;padding-left: 10px;">Content :'.$r["TextTest"].'</td>
						
					</tr>';
			$body.=$str;
			
					
					
			$body.='</tbody>
		            </table>
		            ';
			
								
			$body.='</body>
					</html>';
		
	$body .='<br><table width="100%" style="padding:20px" >
		<tr><td align="right"><b>'.$l_sender.':</b> <b><i>'.$username.'</i></b></td></tr>
		</table>';
	//echo $body;
	
	$mail->Body = $body;
		
	if(!$mail->Send()) {
	  	echo "Mailer Error Elitech: " . $mail->ErrorInfo."<br>";
	} else {
		//mysql_query("UPDATE `pr_testproject` SET `Sendmail`='1' WHERE (`IDTest`='".$r['IDTest']."')");
	 	echo "Sent Project Comment Elitech Successfully <br>";
	}
	
}
for($i=0;$i<count($jpg);$i++){
			unlink("$i.jpg");
		}
mysql_close($connection);?>